/*******************************************************************************
 * 	Copyright 2016 ContainX and OpenStack4j                                          
 * 	                                                                                 
 * 	Licensed under the Apache License, Version 2.0 (the "License"); you may not      
 * 	use this file except in compliance with the License. You may obtain a copy of    
 * 	the License at                                                                   
 * 	                                                                                 
 * 	    http://www.apache.org/licenses/LICENSE-2.0                                   
 * 	                                                                                 
 * 	Unless required by applicable law or agreed to in writing, software              
 * 	distributed under the License is distributed on an "AS IS" BASIS, WITHOUT        
 * 	WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the         
 * 	License for the specific language governing permissions and limitations under    
 * 	the License.                                                                     
 *******************************************************************************/
package com.huawei.openstack4j.openstack.identity.v3.domain;

import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.huawei.openstack4j.model.identity.v3.Domain;
import com.huawei.openstack4j.model.identity.v3.User;
import com.huawei.openstack4j.model.identity.v3.builder.UserBuilder;
import com.huawei.openstack4j.openstack.common.ListResult;

import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;

/**
 * User model class for identity/v3
 *
 * @see <a href= "http://developer.openstack.org/api-ref-identity-v3.html#users-v3">API reference</a>
 */
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonRootName("user")
public class KeystoneUser implements User {

    private static final long serialVersionUID = 1L;
    @JsonProperty
    private String id;
    @JsonProperty
    private String name;
    @JsonProperty
    private KeystoneDomain domain;
    @JsonProperty("domain_id")
    private String domainId;
    private String email;
    private String password;
    private String description;
    @JsonProperty("default_project_id")
    private String defaultProjectId;
    private Map<String, String> links;
    private Boolean enabled = true;
    @JsonProperty("password_expires_at")
    private String passwordExpiresAt;
    private Map<String, String> extra;
    @JsonProperty("pwd_status")
    private Boolean pwdStatus;
    @JsonProperty("pwd_strength")
    private String pwdStrength;
    private String mobile;
    private Boolean forceResetPwd;
    @JsonProperty("last_project_id")
    private String lastProjectId;


    /**
     * @return the user builder
     */
    public static UserBuilder builder() {
        return new UserConcreteBuilder();
    }

    @Override
    public UserBuilder toBuilder() {
        return new UserConcreteBuilder(this);
    }

    /**
     * @return the id of the user
     */
    @Override
    public String getId() {
        return id;
    }

    /**
     * @return the of the user
     */
    @Override
    public String getName() {
        return name;
    }

    /**
     * @return the email of the user
     */
    @Override
    public String getEmail() {
        return email;
    }

    /**
     * @return the mobile of the user
     */
    @Override
    public String getMobile() {
        return mobile;
    }

    /**
     * @return the password of the user
     */
    @Override
    public String getPassword() {
        return password;
    }

    /**
     * @return the password status of the user
     */
    @Override
    public Boolean getPwdStatus() {
        return pwdStatus;
    }

    /**
     * @return the password strength of the user
     */
    @Override
    public String getPwdStrength() {
        return pwdStrength;
    }

    /**
     * @return the forceResetPwd of the user
     */
    @Override
    public Boolean getForceResetPwd() {
        return forceResetPwd;
    }

    /**
     * @return the description of the user
     */
    @Override
    public String getDescription() {
        return description;
    }

    /**
     * @return the domainId of the user
     */
    @Override
    public String getDomainId() {
        return domainId;
    }

    /**
     * @return the domain of the user
     */
    @Override
    public Domain getDomain() {
        return domain;
    }

    /**
     * @return the defaultProjectId of the user
     */
    @Override
    public String getDefaultProjectId() {
        return defaultProjectId;
    }

    /**
     * @return the last project id of the user
     */
    @Override
    public String getLastProjectId() {
        return lastProjectId;
    }

    /**
     * @return the password_expires_at of the user
     */
    @Override
    public String getPasswordExpiresAt() {
        return passwordExpiresAt;
    }

    /**
     * @return the links of the user
     */
    @Override
    public Map<String, String> getLinks() {
        return links;
    }

    /**
     * @return the extra of the user
     */
    @Override
    public Map<String, String> getExtra() {
        return extra;
    }

    /**
     * @return the enabled of the user
     */
    @Override
    public boolean isEnabled() {
        return enabled != null && enabled;
    }

    /**
     * set user enabled
     *
     * @param enabled the new enabled status
     */
    @Override
    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this).omitNullValues()
                .add("name", name)
                .add("id", id)
                .add("email", email)
                .add("password", password)
                .add("description", description)
                .add("domainId", domainId)
                .add("links", links)
                .add("extra",extra)
                .add("enabled", enabled)
                .add("defaultProjectId", defaultProjectId)
                .add("passwordExpiresAt", passwordExpiresAt)
                .add("mobile",mobile)
                .add("pwdStatus", pwdStatus)
                .add("pwdStrength", pwdStrength)
                .add("forceResetPwd", forceResetPwd)
                .add("lastProjectId", lastProjectId)
                .toString();
    }

    /**
    * {@inheritDoc}
    */
   @Override
   public boolean equals(Object obj) {
     if (this == obj) return true;
     if (obj == null || getClass() != obj.getClass()) return false;
     KeystoneUser that = KeystoneUser.class.cast(obj);
     return Objects.equal(this.name, that.name)
           && Objects.equal(this.id, that.id)
           && Objects.equal(this.email, that.email)
           && Objects.equal(this.password, that.password)
           && Objects.equal(this.description, that.description)
           && Objects.equal(this.domainId, that.domainId)
           && Objects.equal(this.links, that.links)
           && Objects.equal(this.enabled, that.enabled)
           && Objects.equal(this.defaultProjectId, that.defaultProjectId);
  }


    public static class Users extends ListResult<KeystoneUser> {

        private static final long serialVersionUID = 1L;
        @JsonProperty("users")
        private List<KeystoneUser> list;

        @Override
        public List<KeystoneUser> value() {
            return list;
        }
    }

    public static class UserConcreteBuilder implements UserBuilder {

        KeystoneUser model;

        UserConcreteBuilder() {
            this(new KeystoneUser());
        }

        UserConcreteBuilder(KeystoneUser model) {
            this.model = model;
        }

        /**
         * @see KeystoneUser#getId()
         */
        @Override
        public UserBuilder id(String id) {
            model.id = id;
            return this;
        }

        /**
         * @return the KeystoneUser model
         */
        @Override
        public User build() {
            return model;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public UserBuilder from(User in) {
            if (in != null)
                this.model = (KeystoneUser) in;
            return this;
        }

        /**
         * @see KeystoneUser#getName()
         */
        @Override
        public UserBuilder name(String name) {
            model.name = name;
            return this;
        }

        /**
         * @see KeystoneUser#getDefaultProjectId()
         */
        @Override
        public UserBuilder defaultProjectId(String defaultProjectId) {
            model.defaultProjectId = defaultProjectId;
            return this;
        }

        /**
         * @see KeystoneUser#getLastProjectId()
         */
        @Override
        public UserBuilder lastProjectId(String lastProjectId) {
            model.lastProjectId = lastProjectId;
            return this;
        }

        /**
         * @see KeystoneUser#getDomainId()
         */
        @Override
        public UserBuilder domainId(String domainId) {
            model.domainId = domainId;
            return this;
        }

        /**
         * @see KeystoneUser#getDomain()
         */
        @Override
        public UserBuilder domain(Domain domain) {
            if (domain != null && domain.getId() != null)
                model.domainId = domain.getId();
            return this;
        }

        /**
         * @see KeystoneUser#getEmail()
         */
        @Override
        public UserBuilder email(String email) {
            model.email = email;
            return this;
        }

        /**
         * @see KeystoneUser#getMobile()
         */
        @Override
        public UserBuilder mobile(String mobile) {
            model.mobile = mobile;
            return this;
        }

        /**
         * @see KeystoneUser#getPassword()
         */
        @Override
        public UserBuilder password(String password) {
            model.password = password;
            return this;
        }

        /**
         * @see KeystoneUser#getPwdStatus()
         */
        @Override
        public UserBuilder pwdStatus(Boolean pwdStatus) {
            model.pwdStatus = pwdStatus;
            return this;
        }

        /**
         * @see KeystoneUser#getPwdStrength()
         */
        @Override
        public UserBuilder pwdStrength(String pwdStrength) {
            model.pwdStrength = pwdStrength;
            return this;
        }

        /**
         * @see KeystoneUser#getPasswordExpiresAt()
         */
        @Override
        public UserBuilder passwordExpiresAt(String passwordExpiresAt) {
            model.passwordExpiresAt = passwordExpiresAt;
            return this;
        }

        /**
         * @see KeystoneUser#getForceResetPwd()
         */
        @Override
        public UserBuilder forceResetPwd(Boolean forceResetPwd) {
            model.forceResetPwd = forceResetPwd;
            return this;
        }

        /**
         * @see KeystoneUser#getLinks()
         */
        @Override
        public UserBuilder links(Map<String, String> links) {
            model.links = links;
            return this;
        }

        /**
         * @see KeystoneUser#getLinks()
         */
        @Override
        public UserBuilder extra(Map<String, String> extra) {
            model.extra = extra;
            return this;
        }

        /**
         * @see KeystoneUser#isEnabled()
         */
        @Override
        public UserBuilder enabled(boolean enabled) {
            model.enabled = enabled;
            return this;
        }

        /**
         * @see KeystoneUser#getDescription()
         */
        @Override
        public UserBuilder description(String description) {
            model.description = description;
            return this;
        }
    }

}
